package io.easyspring.message.core;

import io.easyspring.message.core.events.MessageSendSuccessEvent;
import io.easyspring.message.core.support.EasyMessage;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 消息的事件发送器
 *
 * @author summer
 * @version 2.0.0-RELEASE
 * DateTime 2019-06-17 15:43
 */
@Component
public class MessageEventPublisher {

    /**
     * 注入程序上下文环境
     */
    @Resource
    private ApplicationContext applicationContext;

    /**
     * 封装注册成功事件的发送方法
     *
     * @param source 事件最初发生的对象本身
     * @param message 消息对象
     * Author summer
     * Version 2.0.0-RELEASE
     * DateTime 2019-06-17 15:43
     */
    public <M extends EasyMessage> void publishMessageSendSuccessEvent(Object source, M message){
        // 创建消息发送成功事件
        MessageSendSuccessEvent sendMessageSuccessEvent = new MessageSendSuccessEvent(source, message);
        // 发送事件
        applicationContext.publishEvent(sendMessageSuccessEvent);
    }
}
